package com.ctg.payroll2.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ctg.payroll2.bean.po.PbSecgroup;

public class Recursion {
	private StringBuffer returnStr = new StringBuffer();

	private void recursionFn(List<PbSecgroup> list, PbSecgroup node) {
		if (hasChild(list, node)) {
			returnStr.append("{id:'");
			returnStr.append(node.getGroupCode()).append("'");
			returnStr.append(",text:'");
			returnStr.append(node.getGroupName()).append("'");
			returnStr.append(",children:[");
			List childList = getChildList(list, node);
			Iterator it = childList.iterator();
			while (it.hasNext()) {
				PbSecgroup n = (PbSecgroup) it.next();
				recursionFn(list, n);
			}
			returnStr.append("]},");
		} else {
			returnStr.append("{id:'");
			returnStr.append(node.getGroupCode()).append("'");
			returnStr.append(",text:'");
			returnStr.append(node.getGroupName()).append("'");
			returnStr.append(",leaf:true},");
		}
	}

	private boolean hasChild(List<PbSecgroup> list, PbSecgroup node) { //判断是否有子节点
		return getChildList(list, node).size() > 0 ? true : false;
	}

	private List<PbSecgroup> getChildList(List<PbSecgroup> list, PbSecgroup node) { //得到子节点列表
		List<PbSecgroup> li = new ArrayList<PbSecgroup>();
		Iterator it = list.iterator();
		while (it.hasNext()) {
			PbSecgroup n = (PbSecgroup) it.next();
			if (n.getGroupSupcode().equals(node.getGroupCode())) {
				li.add(n);
			}
		}
		return li;
	}

	private String modifyStr(String returnStr) {//修饰一下才能满足Extjs的Json格式
		return ("[" + returnStr + "]").replaceAll(",]", "]");

	}
	
	public static String getString(List<PbSecgroup> nodeList){
		Recursion r = new Recursion();
		r.recursionFn(nodeList, nodeList.get(0));
		
		return r.modifyStr(r.getReturnStr());
	}

	public String getReturnStr() {
		return returnStr.toString();
	}
	
	public static void main(String[] args) {
		List<PbSecgroup> nodeList = new ArrayList<PbSecgroup>();
		
		PbSecgroup node1 = new PbSecgroup("1", "节点1", "节点1", "0");
		PbSecgroup node2 = new PbSecgroup("2", "节点2", "节点2", "1");
		PbSecgroup node3 = new PbSecgroup("4", "节点4", "节点4", "2");
		PbSecgroup node4 = new PbSecgroup("5", "节点5", "节点5", "2");
		PbSecgroup node5 = new PbSecgroup("6", "节点6", "节点6", "2");
		PbSecgroup node6 = new PbSecgroup("3", "节点3", "节点3", "1");
		PbSecgroup node7 = new PbSecgroup("8", "节点8", "节点8", "3");
		PbSecgroup node8 = new PbSecgroup("10", "节点10", "节点10", "8");
		PbSecgroup node9 = new PbSecgroup("9", "节点9", "节点9", "3");
		PbSecgroup node10 = new PbSecgroup("7", "节点7", "节点7", "1");
		PbSecgroup node11 = new PbSecgroup("11", "节点11", "节点11", "7");
		PbSecgroup node12 = new PbSecgroup("13", "节点13", "节点13", "11");
		PbSecgroup node13 = new PbSecgroup("14", "节点14", "节点14", "13");
		PbSecgroup node14 = new PbSecgroup("15", "节点15", "节点15", "14");
		PbSecgroup node15 = new PbSecgroup("12", "节点12", "节点12", "7");

		nodeList.add(node1);  
		nodeList.add(node2);
		nodeList.add(node3);
		nodeList.add(node4);
		nodeList.add(node5);
		nodeList.add(node6);
		nodeList.add(node7);
		nodeList.add(node8);
		nodeList.add(node9);
		nodeList.add(node10);
		nodeList.add(node11);
		nodeList.add(node12);
		nodeList.add(node13);
		nodeList.add(node14);
		nodeList.add(node15);
		
		System.out.println(getString(nodeList));
	}
}
